package com.ruoyi.duty.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.duty.domain.bo.DutyJypxBo;
import com.ruoyi.duty.domain.vo.DutyJypxVo;
import com.ruoyi.duty.service.IDutyJypxService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 就业培训
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/duty/jypx")
public class DutyJypxController extends BaseController {

    private final IDutyJypxService iDutyJypxService;

    /**
     * 查询就业培训列表
     */
    @GetMapping("/list")
    public TableDataInfo<DutyJypxVo> list(DutyJypxBo bo, PageQuery pageQuery) {
        return iDutyJypxService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出就业培训列表
     */
    @Log(title = "就业培训", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DutyJypxBo bo, HttpServletResponse response) {
        List<DutyJypxVo> list = iDutyJypxService.queryList(bo);
        ExcelUtil.exportExcel(list, "就业培训", DutyJypxVo.class, response);
    }

    /**
     * 获取就业培训详细信息
     *
     * @param jypxId 主键
     */
    @GetMapping("/{jypxId}")
    public R<DutyJypxVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long jypxId) {
        return R.ok(iDutyJypxService.queryById(jypxId));
    }

    /**
     * 新增就业培训
     */
    @SaCheckPermission("duty:jypx:add")
    @Log(title = "就业培训", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DutyJypxBo bo) {
        return toAjax(iDutyJypxService.insertByBo(bo));
    }

    /**
     * 修改就业培训
     */
    @SaCheckPermission("duty:jypx:edit")
    @Log(title = "就业培训", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DutyJypxBo bo) {
        return toAjax(iDutyJypxService.updateByBo(bo));
    }

    /**
     * 删除就业培训
     *
     * @param jypxIds 主键串
     */
    @SaCheckPermission("duty:jypx:remove")
    @Log(title = "就业培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jypxIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] jypxIds) {
        return toAjax(iDutyJypxService.deleteWithValidByIds(Arrays.asList(jypxIds), true));
    }
}
